﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Crm.Workflow;
using System.Workflow.Activities;
using Microsoft.Crm.Sdk;
using System.Workflow.ComponentModel;
using Microsoft.Crm.SdkTypeProxy;
using System.Diagnostics;
using Microsoft.Crm.Sdk.Query;
using Infusion.CRM.WF.CRM40WSX.Proxy;

namespace Infusion.CRM.WF.CRM40WSX.Activity
{

    /// <summary>
    /// This workflow activity (which can be run against any entity) validates
    /// a given email address by querying the WebserviceX.Net Validate Email Address Web Service.
    /// </summary>
    [CrmWorkflowActivity("Validate Email", "WebserviceX.Net")]
    public class ValidateEmailActivity : SequenceActivity
    {

        #region Workflow Parameters

        /// <summary>
        /// Input parameter: Email Address.
        /// </summary>
        public static DependencyProperty EmailAddressProperty = DependencyProperty.Register("EmailAddress", typeof(string), typeof(ValidateEmailActivity));
        [CrmInput("Email Address")]
        public string EmailAddress
        {
            get
            {
                return (string)base.GetValue(EmailAddressProperty);
            }
            set
            {
                base.SetValue(EmailAddressProperty, value);
            }
        }

        /// <summary>
        /// Output Parameter: True if the Email is valid.
        /// </summary>
        public static DependencyProperty IsValidProperty = DependencyProperty.Register("IsValid", typeof(CrmBoolean), typeof(ValidateEmailActivity));
        [CrmOutput("Is Valid")]
        public CrmBoolean IsValid
        {
            get
            {
                return (CrmBoolean)base.GetValue(IsValidProperty);
            }
            set
            {
                base.SetValue(IsValidProperty, value);
            }
        }

        /// <summary>
        /// Output Parameter: True if operation was successful. If False, Error Message will be populated.
        /// </summary>
        public static DependencyProperty OperationSuccessfulProperty = DependencyProperty.Register("OperationSuccessful", typeof(CrmBoolean), typeof(ValidateEmailActivity));
        [CrmOutput("Operation Successful")]
        public CrmBoolean OperationSuccessful
        {
            get
            {
                return (CrmBoolean)base.GetValue(OperationSuccessfulProperty);
            }
            set
            {
                base.SetValue(OperationSuccessfulProperty, value);
            }
        }

        /// <summary>
        /// Output Parameter: Error Message is only populated when Operation Successful == false.
        /// </summary>
        public static DependencyProperty ErrorMsgProperty = DependencyProperty.Register("ErrorMsg", typeof(string), typeof(ValidateEmailActivity));
        [CrmOutput("Error Message")]
        public string ErrorMsg
        {
            get
            {
                return (string)base.GetValue(ErrorMsgProperty);
            }
            set
            {
                base.SetValue(ErrorMsgProperty, value);
            }
        }

        #endregion

        #region SequenceActivity

        /// <summary>
        /// Validate Email Address
        /// </summary>
        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            try
            {
                this.IsValid = new CrmBoolean(new ValidateEmailProxy().Validate(this.EmailAddress));
                this.OperationSuccessful = new CrmBoolean(true);
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.ToString());
                this.ErrorMsg = ex.Message;
                this.OperationSuccessful = new CrmBoolean(false);
            }

            return ActivityExecutionStatus.Closed;
        }

        #endregion

    }

}
